package aspect;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;

@Aspect
public class aspectjAdvice {

    @Pointcut("execution(* advice.AdviceTarget.*(..))")
    public void piont() {

    }
    @Before("piont()")
    public void aspectBefore(JoinPoint joinPoint) {
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>前置增强启动>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        Object[] args = joinPoint.getArgs();
        Object target = joinPoint.getTarget();
        Signature signature = joinPoint.getSignature();
        System.out.println("目标类：" + target);
        System.out.println("目标方法:" + signature.getName());
        System.out.println("参数列表：");
        for (Object arg : args) {
            System.out.println(arg);
        }
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>前置增强结束>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    }

    @AfterReturning(value = "piont()", returning = "returnVal")
    public void aspectBefore(JoinPoint joinPoint, String returnVal) {
        System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<后置增强启动<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
        Object[] args = joinPoint.getArgs();
        Object target = joinPoint.getTarget();
        Signature signature = joinPoint.getSignature();
        System.out.println("目标类：" + target);
        System.out.println("目标方法:" + signature.getName());
        System.out.println("参数列表：");
        for (Object arg : args) {
            System.out.println(arg);
        }
        System.out.println("返回值：" + returnVal);
        System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<后置增强结束<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
    }

    @AfterThrowing(value = "piont()", throwing = "e")
    public void aspectAfterThrowing(JoinPoint joinPoint, Exception e) {
        System.out.println("!!!!!!!!!!!!!!!!!!异常抛出增强启动!!!!!!!!!!!!!!!!!!!!!");
        Object[] args = joinPoint.getArgs();
        Object target = joinPoint.getTarget();
        Signature signature = joinPoint.getSignature();
        System.out.println("目标类：" + target);
        System.out.println("目标方法:" + signature.getName());
        System.out.println("参数列表：");
        for (Object arg : args) {
            System.out.println(arg);
        }
        System.out.println(e.toString());
        System.out.println("!!!!!!!!!!!!!!!!!!!!!异常抛出增强结束!!!!!!!!!!!!!!!!!!!!!");
    }

    @After(value = "piont()")
    public void aspectAfter() {
        System.out.println(">>>>>>>>>>>>>>>>>after<<<<<<<<<<<<<<<<<<<<<<<<<<");
    }

    @Around(value = "piont()")
    public void aspectAround(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("[[[[[[[[[[[[[[[[[[[[[[[[[环绕增强启动[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[");
        try {
            Object[] args = joinPoint.getArgs();
            Object target = joinPoint.getTarget();
            Signature signature = joinPoint.getSignature();
            System.out.println("目标类：" + target);
            System.out.println("目标方法:" + signature.getName());
            System.out.println("参数列表：");
            for (Object arg : args) {
                System.out.println(arg);
            }
            Object proceed = joinPoint.proceed();
            System.out.println("返回值："+proceed.toString());
            System.out.println("]]]]]]]]]]]]]]]]]]]]]]]]]]]]环绕增强结束]]]]]]]]]]]]]]]]]]]]]]]]");
        } catch (Exception e) {
            System.out.println("!!!!环绕增强抛出异常：");
            System.out.println(e.getMessage());
            throw e;
        }
    }
}
